package com.sangcomz.fishbun.ui.detail.ui;

import com.sangcomz.fishbun.BaseAbilitySlice;
import com.sangcomz.fishbun.Fishton;
import com.sangcomz.fishbun.ResourceTable;
import com.sangcomz.fishbun.adapter.image.ImageAdapter;
import com.sangcomz.fishbun.datasource.FishBunDataSourceImpl;
import com.sangcomz.fishbun.ui.detail.DetailImageContract;
import com.sangcomz.fishbun.ui.detail.adapter.DetailViewPagerAdapter;
import com.sangcomz.fishbun.ui.detail.model.DetailImageRepositoryImpl;
import com.sangcomz.fishbun.ui.detail.model.DetailImageViewData;
import com.sangcomz.fishbun.ui.detail.mvp.DetailImagePresenter;
import com.sangcomz.fishbun.util.*;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.utils.net.Uri;

import java.io.IOException;
import java.util.List;

public class DetailImageAbilitySlice extends BaseAbilitySlice implements DetailImageContract.View, PageSlider.PageChangedListener {

    private DetailImageContract.Presenter presenter;

    private RadioWithTextButton btnDetailCount;
    private PageSlider vpDetailPager;
    private Button btnDetailBack;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_detail_image_layout);

        initView();

        presenter = createPresenter();
        int intParam = intent.getIntParam(DetailImageAbility.INIT_IMAGE_POSITION, -1);
        System.out.println("===DetailImageAbilitySlice===>" + intParam);
        presenter.handleOnCreate(intParam);
    }

    private void initView() {
        vpDetailPager = (PageSlider) findComponentById(ResourceTable.Id_vp_detail_pager);
        btnDetailCount = (RadioWithTextButton) findComponentById(ResourceTable.Id_btn_detail_count);
        btnDetailBack = (Button) findComponentById(ResourceTable.Id_btn_detail_back);

        vpDetailPager.addPageChangedListener(this);
        btnDetailBack.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                finishActivity();
            }
        });
    }

    private DetailImagePresenter createPresenter() {
        return new DetailImagePresenter(this, new DetailImageRepositoryImpl(new FishBunDataSourceImpl(Fishton.getInstance())));
    }

    @Override
    public void unselectImage() {
        if (btnDetailCount != null) {
            btnDetailCount.unselect();
        }
    }

    @Override
    public void updateRadioButtonWithText(String text) {
        //FIXME Need to fix to work without post
//        new MainUiHandler().run(() -> btnDetailCount.setText(text));
        btnDetailCount.setText(text);
    }

    @Override
    public void updateRadioButtonWithDrawable() {
        if (btnDetailCount == null) return;
        try {
            PixelMapElement element = new PixelMapElement(getResourceManager().getResource(ResourceTable.Media_icon_select));
            btnDetailCount.setDrawable(element);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void finishActivity() {
        Intent i = new Intent();
        getAbility().setResult(RESULT_OK, i);
        terminateAbility();
    }

    @Override
    public void finishAndShowErrorToast() {
        try {
            String msg = getResourceManager().getElement(ResourceTable.String_msg_error).getString();
//            new ToastDialog(this).setText(msg).show();
            ToastUtil.showToast(this, msg);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        terminate();
    }

    @Override
    public void initViewPagerAdapter(ImageAdapter imageAdapter) {
        if (vpDetailPager == null) return;
        vpDetailPager.setProvider(new DetailViewPagerAdapter(imageAdapter, this));
    }

    @Override
    public void showImages(int initPosition, List<Uri> pickerImages) {
        if (vpDetailPager == null) return;
        PageSliderProvider provider = vpDetailPager.getProvider();
        if (provider instanceof DetailViewPagerAdapter) {
            System.out.println("===showImages===>"+initPosition);
            ((DetailViewPagerAdapter) provider).setImages(pickerImages);
            vpDetailPager.setCurrentPage(initPosition);
        }
    }

    @Override
    public void showSnackbar(String message) {
//        new ToastDialog(this).setText(message).show();
        ToastUtil.showToast(this, message);
    }

    @Override
    public void setBackButton() {
        if (btnDetailBack != null) {
            btnDetailBack.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    finishActivity();
                }
            });
        }
    }

    @Override
    public void setToolBar(DetailImageViewData detailImageViewData) {
        UiUtil.setStatusBarColor(getWindow(), Color.BLACK.getValue());
    }

    @Override
    public void setCountButton(DetailImageViewData detailImageViewData) {
        if (btnDetailCount != null) {
            btnDetailCount.unselect();
            btnDetailCount.setCircleColor(detailImageViewData.getColorActionBar());
            btnDetailCount.setTextColor(detailImageViewData.getColorActionBarTitle());
            btnDetailCount.setStrokeColor(detailImageViewData.getColorSelectCircleStroke());
            btnDetailCount.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    presenter.onCountClick(vpDetailPager.getCurrentPage());
                }
            });
        }
    }

    @Override
    public void onPageSliding(int i, float v, int i1) {

    }

    @Override
    public void onPageSlideStateChanged(int i) {

    }

    @Override
    public void onPageChosen(int position) {
        presenter.changeButtonStatus(position);
    }

    @Override
    protected void onBackPressed() {
        finishActivity();
    }

    @Override
    protected void onStop() {
        vpDetailPager.removePageChangedListener(this);
        super.onStop();
    }
}
